package com.hailiao.beidou.message.data.message

import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.hailiao.beidou.message.data.db.DB

/**
 * 消息数据仓库
 * @Author d10ng
 * @Date 2024/3/2 14:51
 */
object MessageRepository {

    private val messageDao by lazy { DB.instant.messageDao() }

    /**
     * 获取消息分页数据
     * @param number String
     * @return Flow<PagingData<MessageEntity>>
     */
    fun getPageDataFlow(number: String) = Pager(
        config = PagingConfig(
            pageSize = 20,
            enablePlaceholders = true
        ),
        pagingSourceFactory = { messageDao.pagingSource(number) }
    ).flow

    /**
     * 模糊查询消息数量
     * @param key String
     * @return List<MatchedMessageCount>
     */
    suspend fun searchCount(key: String) = messageDao.susQueryCount(key)

    /**
     * 查询消息
     * @param number String
     * @param key String
     * @return List<MessageEntity>
     */
    suspend fun search(number: String, key: String) = messageDao.susQuery(number, key)

    /**
     * 查询指定时间之前的消息数量
     * @param number String
     * @param time Long
     * @return Long
     */
    suspend fun countByTime(number: String, time: Long) = messageDao.susQueryPosition(number, time)
}